package com.yantong.controller.finance;

import com.yantong.annotation.Log;
import com.yantong.constant.RefundStatus;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.DateUtils;
import com.yantong.utils.PageUtils;
import com.yantong.utils.excel.ExcelHelper;
import com.yantong.pojo.Order;
import com.yantong.pojo.OrderRefund;
import com.yantong.service.RefundService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 财务退款申请控制类
 *
 * @author kanghaixin
 * @date 2018/9/17.15:47
 */
@Controller
@RequestMapping("/finance/refund")
public class FinanceRefundController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(FinanceRefundController.class);
    private static final String prefix = "finance/refund";

    @Autowired
    private RefundService refundService;

    /**
     * 申请退款列表
     *
     * @param model
     * @return
     */
    @RequiresPermissions("finance:refund:refund")
    @GetMapping()
    public String refunds(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/refunds";
    }

    /**
     * 退款申请列表数据
     *
     * @param start
     * @param end
     * @param centreId
     * @param queryItem
     * @param status
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequiresPermissions("finance:refund:refund")
    @GetMapping("/list")
    public PageUtils<OrderRefund> listOrderRefunds(String start, String end, Long centreId, String queryItem, Integer status,
                                                   int offset, int limit) {
        return refundService.listOrderRefund(DateUtils.parseDateForm(start), DateUtils.parseDateForm(end), centreId, getUserTeamId(), queryItem, status, offset, limit);
    }


    /**
     * 退款申请导出
     *
     * @param start
     * @param end
     * @param centreId
     * @param queryItem
     * @param status
     */
    @RequiresPermissions("finance:refund:export")
    @GetMapping("/export")
    public void exportRefunds(String start, String end, Long centreId, String queryItem, Integer status,
                              HttpServletResponse response) {
        List<List<Object>> resultData = new ArrayList<>();
        String[] head = {"订单编号", "退款编号", "退款支付单号", "退款商户单号", "订单创建时间", "退款申请时间", "下单人", "下单人ID",
                "收货人", "手机号码", "订单金额", "实际支付金额", "申请退款金额", "退款到微信", "退款到余额", "审核人", "审核时间",
                "退款人", "退款时间", "退款状态"};
        resultData.add(0, Arrays.asList(head));
        List<OrderRefund> list = refundService.listOrderRefund(DateUtils.parseDateForm(start), DateUtils.parseDateForm(end), centreId, getUserTeamId(), queryItem, status);
        if (CollectionUtils.isNotEmpty(list)) {
            for (OrderRefund or : list) {
                if (or == null) {
                    continue;
                }
                Order order = or.getOrder();
                List<Object> row = new ArrayList<>();
                row.add(order == null ? "" : order.getOrdersn());
                row.add(or.getRefundsn());
                row.add(or.getRefundNo());
                row.add(or.getRefundMerchNo());
                row.add(order == null ? "" : order.getCreateTime());
                row.add(or.getCreateTime());
                row.add(order == null ? "" : order.getNickname());
                row.add(order == null ? "" : order.getMid());
                row.add(order == null ? "" : order.getSname());
                row.add(order == null ? "" : order.getMobile());
                row.add(or.getOrderprice());
                row.add(or.getPayprice());
                row.add(or.getApplyprice());
                row.add(or.getRefundprice());
                row.add(or.getRefundcredit2());
                row.add(or.getCheckname());
                row.add(or.getCheckTime());
                row.add(or.getRefundname());
                row.add(or.getRefundTime());
                row.add(RefundStatus.get(or.getStatus()));
                resultData.add(row);
            }
        }
        String fileName = "退款申请_" + DateUtils.format(new Date(), "yyyyMMddHHmmss");
        try {
            ExcelHelper.exportExcel(response, fileName, "退款申请", resultData);
        } catch (IOException e) {
            logger.info("export refund log error:{}", e.getMessage());
        }

    }


//    /**
//     * 确认退款
//     *
//     * @param refundId
//     * @return
//     */
//    @Log(module = "退款申请管理", operation = "确认退款")
//    @ResponseBody
//    @PostMapping("/confirmRefund")
//    @RequiresPermissions("finance:refund:confirmRefund")
//    public ResultVo refund(Long refundId) {
//        if (refundId == null) {
//            return ResultVo.error();
//        }
//        if (refundService.confirmRefund(refundId, getUserId(), getNikeName()) < 1) {
//            return ResultVo.error();
//        }
//        return ResultVo.ok();
//    }

//    /**
//     * 批量退款
//     *
//     * @param ids
//     * @return
//     */
//    @Log(module = "退款申请管理", operation = "批量退款")
//    @ResponseBody
//    @PostMapping("/batchRefund")
//    @RequiresPermissions("finance:refund:batchRefund")
//    public ResultVo batchRefund(@RequestParam("ids[]") Long[] ids) {
//        if (ids == null || ids.length < 1) {
//            return ResultVo.error();
//        }
//        for (Long id : ids) {
//            if (id == null) {
//                continue;
//            }
//            refundService.confirmRefund(id, getUserId(), getNikeName());
//        }
//        return ResultVo.ok();
//    }


}
